<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var longestNiceSubstring = function (s) {
      let max = -1, pos = [1, 1];
      for (let i = 0; i < s.length; i++) {
        for (let j = 1; j <= s.length; j++) {
          const str = s.slice(i, j);
          const size = new Set(str.split('')).size;
          const upsize = new Set(str.toUpperCase().split('')).size;
          if (size === upsize * 2) {
            if ((j - i) > max) {
              max = j - i;
              pos = [i, j];
            }
          }
        }
      }
      return s.slice(...pos);
    };
    console.log(longestNiceSubstring("YazaAay"))
  </script>
</body>

</html>